package com.tengju.bff.interfaces.manage.crm.user.facade;

import com.tengju.bff.interfaces.app.domination.vo.StarLevelNumVO;
import com.tengju.bff.interfaces.manage.crm.user.assembler.CrmDominationAssembler;
import com.tengju.data.domain.model.clickhouse.star.DominationMemberNew;
import com.tengju.data.domain.model.clickhouse.star.DominationSigningMember;
import com.tengju.data.domain.model.clickhouse.star.DominationUpComing;
import com.tengju.data.domain.model.user.*;
import com.tengju.data.domain.shared.DateUtil;
import com.tengju.user.application.user.UserInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class CrmDominationTeamFacade {

    private final DominationDetailRepository dominationDetailRepository;
    private final UserInfoService userInfoService;

    public List<StarLevelNumVO> memberQuantity(UserIdCode idCode) {
        DominationDetail dominationDetail = dominationDetailRepository.getByIdCode(idCode);
        return CrmDominationAssembler.assemblerToVO(dominationDetail);
    }

    public List<StarLevelNumVO> memberNewNum(UserIdCode idCode) {
        //获取当前用户管辖等级
        com.tengju.user.domain.model.user.DominationLevelEnum dominationLevelEnum =
                userInfoService.getDominationLevel(new com.tengju.user.domain.model.user.UserIdCode(idCode.getValue()));
        DominationLevelEnum dominationLevel =
                DominationLevelEnum.fromVal(dominationLevelEnum.getVal());

        //本月时间区间
        LocalDateTime timeFrom = DateUtil.getFirstDayOfMonthMin();
        LocalDateTime timeTo = LocalDateTime.now();

        //count
        DominationMemberNew dominationMemberNew =
                dominationDetailRepository
                        .countNewMemberCurrentMonth(idCode, dominationLevel, null, timeFrom, timeTo);

        return CrmDominationAssembler.assemblerToVO(dominationMemberNew);
    }


    public List<StarLevelNumVO> signingMember(UserIdCode idCode) {
        //获取当前用户管辖等级
        com.tengju.user.domain.model.user.DominationLevelEnum dominationLevelEnum =
                userInfoService.getDominationLevel(new com.tengju.user.domain.model.user.UserIdCode(idCode.getValue()));
        DominationLevelEnum dominationLevel =
                DominationLevelEnum.fromVal(dominationLevelEnum.getVal());
        //count
        DominationSigningMember dominationSigningMember = dominationDetailRepository
                .countSigningMemberByIdCodesDominationLevel(List.of(idCode), dominationLevel);
        return CrmDominationAssembler.assemblerToVO(dominationSigningMember);
    }


    public List<StarLevelNumVO> upcomingMember(UserIdCode idCode) {
        //获取当前用户管辖等级
        com.tengju.user.domain.model.user.DominationLevelEnum dominationLevelEnum =
                userInfoService.getDominationLevel(new com.tengju.user.domain.model.user.UserIdCode(idCode.getValue()));
        DominationLevelEnum dominationLevel =
                DominationLevelEnum.fromVal(dominationLevelEnum.getVal());
        //count
        DominationUpComing dominationUpComing = dominationDetailRepository
                .countUpComingMemberByIdCodes(List.of(idCode), dominationLevel);
        return CrmDominationAssembler.assemblerToVO(dominationUpComing);
    }

}